package com.xxx.filter;

import com.xxx.util.BaseContext;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;


@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        String uri = req.getRequestURI();
//        System.out.println(uri);
        String[] uris = {"/basis/login/login.html","css","/img","/js","okLogin"};

        for (String s : uris) {
            if(uri.contains(s)){
                chain.doFilter(request, response);
                return;
            }
        }
        Long id = (Long) req.getSession().getAttribute("employeeId");
        if (null != id){
            BaseContext.setCurrentId(id);
            chain.doFilter(request, response);
        }else {
            resp.sendRedirect("/basis/login/login.html");
        }


    }
}
